//
//  DataBaseHandler.m
//  UI数据库_12.8
//
//  Created by GYRu on 14-12-8.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DataBaseHandler.h"
#import "AlbumDatailsOfList.h"

@implementation DataBaseHandler


+(DataBaseHandler *)shareInstance
{
    // static 静态的 语句只走一次
    // 当第一次制定的时候会产生一个空指针
    static DataBaseHandler *handler = nil;
    // 对指针进行判断 当第一次执行的时候创建一个对象
    if (handler == nil) {
        // 单例不需要release
        handler = [[DataBaseHandler alloc] init];
    }
    // 无论是创建的还是已存在的 都在这里return出去
    return handler;
}


//-(BOOL)openDB:(NSString *)uid
//{
//    // 打开数据库
//    
//    // 1.拼接一个数据库文件路径
//    // documents 文件夹
//    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    // 在 documents 文件夹下拼接一个数据库文件地址
//    NSLog(@"%@", docPath);
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    NSString *folderPath = [docPath stringByAppendingFormat:@"/%@", uid];
//    BOOL folder = [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
//    
//    
//    NSString *dbPath = [folderPath stringByAppendingPathComponent:@"SuperRadio.db"];
//    
//    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
//    
//    
//    
//    return [self judgeResultWith:result text:@"open"];
//    
//    
//}


-(BOOL)openDB
{
    // 打开数据库
    
    // 1.拼接一个数据库文件路径
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *dbPath = [docPath stringByAppendingPathComponent:@"SuperRadio.db"];
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    
    
    
    return [self judgeResultWith:result text:@"open"];
    
    
}



-(BOOL)closeDB
{
    // 关闭数据库
    
    int result = sqlite3_close(dbPoint);
    
    return [self judgeResultWith:result text:@"close"];
    
}

-(BOOL)judgeResultWith:(int)result text:(NSString *)text
{
    if (result == SQLITE_OK) {
        NSLog(@"%@ success", text);
        return YES;
    } else {
        NSLog(@"错误编码: %d", result);
        NSLog(@"%@ faith", text);
        return NO;
    }
}

#pragma mark 创建表
-(BOOL)createCollectTable
{
    // 创建表
    
    // 执行SQL语句的创建函数
    NSString *sql = @"create table collect(title text, trackId text primary key, playUrl64 text, playUrl32 text)";
    
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResultWith:result text:@"create table"];
    
}

#pragma mark 添加数据信息
-(BOOL)insertCollect:(AlbumDatailsOfList *)album
{
    
    // 拼接SQL语句
    NSString *sql = [NSString stringWithFormat:@"insert into collect values('%@', '%@', '%@', '%@')", album.title, [album.trackId stringValue], album.playUrl64, album.playUrl32];
    
    // 执行SQL语句 判断结果
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    

    
    return [self judgeResultWith:result text:@"insert"];
    
}


#pragma mark 查询数据库
-(NSMutableArray *)selectAll
{
    // 查询
    
    // 1.创建一个数据库的替身 存储对数据库的所有操作
    sqlite3_stmt *stmt = nil;
    
    // 2.准备sql语句并执行 将结果保存在stmt中
    NSString *sql = @"select * from collect";
    
    int result = sqlite3_prepare_v2(dbPoint, [sql UTF8String], -1, &stmt, NULL);
    
    // 3.创建一个可变数组 用于存储数据
    NSMutableArray *collectArr = [NSMutableArray array];
    
    // 4.遍历所有的数据 创建相应的学生对象
    if (result == SQLITE_OK) {
        
        // 当sql执行成功 遍历数据
        // 循环遍历所有的结果 每次遍历到一条数据 都会返回SQLITE_ROW 如果没有数据了 就不会返回SQLITE_ROW 跳出循环
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            // 在循环体中去按列取数据

            // text类型的数据取法
            const unsigned char *titleChar = sqlite3_column_text(stmt, 0);
            // 将const unsigned char *字符串转为NSString字符串
            NSString *title = [NSString stringWithUTF8String:(const char *)titleChar];
            
            const unsigned char *idChar = sqlite3_column_text(stmt, 1);
            NSString *trackId = [NSString stringWithUTF8String:(const char *)idChar];
            
            const unsigned char *Url64Char = sqlite3_column_text(stmt, 2);
            NSString *Url64 = [NSString stringWithUTF8String:(const char *)Url64Char];
            
            const unsigned char *Url32Char = sqlite3_column_text(stmt, 3);
            NSString *Url32 = [NSString stringWithUTF8String:(const char *)Url32Char];
            
            
            AlbumDatailsOfList *albumCollect = [[AlbumDatailsOfList alloc] init];
            // 拿获取到的数据 给对象赋值
            albumCollect.title = title;
            albumCollect.trackId = [NSNumber numberWithInt:[trackId intValue]];
            albumCollect.playUrl64 = Url64;
            albumCollect.playUrl32 = Url32;
            
            // 将对象添加到数组中
            [collectArr addObject:albumCollect];
            [albumCollect release];
        }
    }
    // 销毁替身 把里面的操作和结果写入本地sqlite文件
    sqlite3_finalize(stmt);
    
    return collectArr;
}


//-(BOOL)updateStudent:(AlbumDatailsOfList *)stu withName:(NSString *)name
//{
//    // 修改
//    
//    NSString *sql = [NSString stringWithFormat:@"update student set name = '%@' where name = '%@'",  name, stu.name];
//    
//    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
//    return [self judgeResultWith:result text:@"update"];
//    
//}

#pragma mark 删除数据
-(BOOL)deleteCollectWithTrackId:(NSString *)trackId
{
    // 删除
    
    NSString *sql = [NSString stringWithFormat:@"delete from collect where trackId = '%@'", trackId];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    
    return [self judgeResultWith:result text:@"delete"];
    
}


@end
